%{

#include <stdio.h>
#include <string.h>
#include "cue_lexer_parser.h"

#define YYDEBUG 1

struct parse_ctx {
	struct cue_cd *cd;
};

void
yyerror(struct parse_ctx *ctx, void *scanner, char *s) {
	fprintf (stderr, "--> %s\n", s);
}

%}

%union {
	char	*sval;
	int	ival;
	struct cue_cdtime timeval;
}
%destructor { free($$); } STRING MCN

%pure-parser
%parse-param { struct parse_ctx *ctx }
%parse-param { void *scanner }
%lex-param { yyscan_t scanner }

%token <ival> INTEGER
%token <sval> STRING
%token <sval> MCN
%token <timeval> CDTIME

%token FLAGS FOURCH SCMS INDEX ISRC POSTGAP PREGAP CDTEXTFILE TITLE MP3 DCP PRE REM TRACK CDG MOTOROLA
%token AUDIO ISRCCODE PERFORMER SONGWRITER MODE1_2048 MODE1_2352 MODE1_2336 MODE2_2336 MODE2_2352 CATALOG
%token CDI_2336 FILETOKEN CDI_2352 BINARY AIFF WAVE

%%

CueFile : /* empty */
	| Commands
;

Commands :
	Commands Command
	| Command

Command :
	  CatalogCommand
	| CdTextFileCommand
	| FileCommand
	| FlagsCommand
	| IndexCommand
	| IsrcCommand
	| PerformerCommand
	| PostGapCommand
	| PreGapCommand
	| RemCommand
	| SongwriterCommand
	| TitleCommand
	| TrackCommand
;

CatalogCommand :
	CATALOG MCN;
;

CdTextFileCommand :
	CDTEXTFILE STRING;
;

FileCommand :
	FILETOKEN STRING FileType
;

FileType :
	  BINARY
	| MOTOROLA
	| AIFF
	| WAVE
	| MP3
;

FlagsCommand:
	FLAGS Flags
;

Flags :
	Flags Flag
;

Flag :
	  DCP
	| FOURCH
	| PRE
	| SCMS
;

IndexCommand :
	INDEX INTEGER CDTIME
;

IsrcCommand :
	ISRC ISRCCODE
;

PerformerCommand :
	PERFORMER STRING
;

PostGapCommand :
	POSTGAP CDTIME
;

PreGapCommand :
	PREGAP CDTIME
;

RemCommand :
	REM Comment
;

Comment : /* empty */
	| Comment STRING
;

SongwriterCommand :
	SONGWRITER STRING
;

TitleCommand :
	TITLE STRING
;

TrackCommand :
	TRACK INTEGER TrackDataType
;

TrackDataType :
	  AUDIO
	| CDG
	| MODE1_2048
	| MODE1_2352
	| MODE1_2336
	| MODE2_2336
	| MODE2_2352
	| CDI_2336
	| CDI_2352
;

%%

struct cue_cd *
parse_cue_buffer(char const *buffer, size_t buffer_size, int *error)
{
	void *scanner;
	struct parse_ctx ctx;
	struct membuf_lex membuf;

	memset(&ctx, 0, sizeof(ctx));
	memset(&membuf, 0, sizeof(membuf));
	membuf.buffer = buffer;
	membuf.bufsize = buffer_size;

	yylex_init_extra(&membuf, &scanner);
	yyset_debug(1, scanner);
	yyparse(&ctx, scanner);
	yylex_destroy(scanner);

	return NULL;
}

/*int
main(int argc, char **argv)
{
	printf("starting...\n");
	yydebug = 1;

	void *scanner;
	yylex_init(&scanner);
	yyset_debug(1, scanner);
	yyparse(scanner);
	yylex_destroy(scanner);

}*/

